/**
 * 音乐模型
 */
class SearchDemo {
    /**
     * 构造音乐对象
     * @param  {number} id       标识
     * @param  {string} name     关键词
     * @return {SearchDemo}      搜索
     */
    constructor(id, word) {
        this.id = id
        this.word = word
    }

    static find(limit = 0) {
        if (limit > 0) {
            const list = storage.sort(function(a, b) {
                return b.id - a.id
            })
            return list.slice(0, limit)
        } else {
            return storage
        }

    }


    static findOne(id) {
        return storage.find(s => s.id === id)
    }

    delete() {
        return storage.splice(storage.indexOf(this), 1)
    }

    save() {
        storage.indexOf(this) === -1 && storage.push(this)
        return true
    }

    update() {
        return true
    }

    search(word) {
        var regex = new RegExp(word);
        var list = [];
        for (i = 0; i < storage.length; i++) {
            if (regex.test(storage[i].word)) {
                list.push(storage[i])
            }
        }
        return list
    }
}
const storage = [
    new SearchDemo(1, '关键词'),
    new SearchDemo(2, '关键词'),
    new SearchDemo(3, '关键词'),
    new SearchDemo(4, '关键词'),
    new SearchDemo(5, '关键词'),
    new SearchDemo(6, '关键词'),
    new SearchDemo(7, '关键词'),
    new SearchDemo(8, '关键词'),
    new SearchDemo(9, '关键词'),
    new SearchDemo(10, '关键词'),
    new SearchDemo(11, '关键词'),
    new SearchDemo(12, '关键词'),
    new SearchDemo(13, '关键词'),
    new SearchDemo(14, '关键词'),
    new SearchDemo(15, '关键词'),
    new SearchDemo(16, '关键词'),
    new SearchDemo(17, '关键词'),
    new SearchDemo(18, '关键词'),
    new SearchDemo(19, '关键词'),
    new SearchDemo(20, '关键词'),
]

module.exports = SearchDemo